package com.dwxt.community.service;

import com.dwxt.common.base.BaseResult;
import com.dwxt.common.util.DateUtils;
import com.dwxt.community.admin.service.IntegralServiceAdmin;
import com.dwxt.community.dao.AppDataMapper;
import com.dwxt.community.entity.AppData;
import com.dwxt.community.entity.AppDataExample;
import com.dwxt.community.entity.Graph;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class GraphServie {

    @Autowired
    private AppDataMapper appDataMapper;

    @Autowired
    IntegralServiceAdmin integralServiceAdmin;

    public BaseResult selectUserGraph() {
        List<Map> list = selectAppDataGraph();
        List<Map> mapList = integralServiceAdmin.selectTotalIntegralStatistics();
        Integer plus = Integer.parseInt(mapList.get(0).get("plus").toString());
        Integer negative = Integer.parseInt(mapList.get(0).get("negative").toString());
        Map mapPlus = new HashMap();
        mapPlus.put("type", 4);
        mapPlus.put("total", plus);
        list.add(mapPlus);

        Map mapNegative = new HashMap();
        mapNegative.put("type", 5);
        mapNegative.put("total", negative);
        list.add(mapNegative);
        return BaseResult.ok(list);
    }

    private List<Map> selectAppDataGraph() {
        return appDataMapper.selectAppDataGraph();
    }

    public BaseResult selectUserGraphEveryDay() {

        Date now = new Date();
        Date startTime = DateUtils.getPastDate(now, 31);

        List<String> xList = new ArrayList<>();
        List<Integer> downloadList = new ArrayList<>();
        List<Integer> vipList = new ArrayList<>();
        List<Integer> actList = new ArrayList<>();

        Map downloadMap = new HashMap<>();
        Map vipMap = new HashMap<>();
        Map actMap = new HashMap<>();
        for (Date start = startTime; start.getTime() < now.getTime() - 12 * 60 * 60 * 1000; start =
                new Date(start.getTime() + 24 * 60 * 60 * 1000)) {
            int year = DateUtils.getYear(start);
            int day = DateUtils.getDay(start);
            int month = DateUtils.getMonth(start);
            xList.add(DateUtils.formatDate(start));
            AppData appData = selectAppData(year, month, day, 1);
            downloadList.add(appData == null ? 0 : appData.getNumber());

            appData = selectAppData(year, month, day, 2);
            vipList.add(appData == null ? 0 : appData.getNumber());

            appData = selectAppData(year, month, day, 3);
            actList.add(appData == null ? 0 : appData.getNumber());
        }

        downloadMap.put("data", downloadList);
        downloadMap.put("name", "下载量");
        vipMap.put("data", vipList);
        vipMap.put("name", "会员人数");
        actMap.put("data", actList);
        actMap.put("name", "活跃用户数");
        List<Map> yList = new ArrayList<>();
        yList.add(downloadMap);
        yList.add(vipMap);
        yList.add(actMap);

        return BaseResult.ok(Graph.builder().xList(xList).yList(yList).build());

    }

    private AppData selectAppData(int year, int month, int day, int type) {
        AppDataExample example = new AppDataExample();
        example.createCriteria().andYearEqualTo(year).andMonthEqualTo(month).andDayEqualTo(day).andTypeEqualTo(type);
        List<AppData> appData = appDataMapper.selectByExample(example);
        return appData.size() > 0 ? appData.get(0) : null;
    }

    public BaseResult selectIntegralGraph() {
        int year = DateUtils.getYear(null);
        int today = DateUtils.getDay(null);
        int month = DateUtils.getMonth(null);
        List<String> xList = new ArrayList<>();
        List<Integer> downloadList = new ArrayList<>();
        List<Integer> vipList = new ArrayList<>();
        List<Integer> actList = new ArrayList<>();

        Map downloadMap = new HashMap<>();
        Map vipMap = new HashMap<>();
        Map actMap = new HashMap<>();
        for (int day = 1; day <= today; day++) {
            xList.add(day + "");
            String date = year + "-" + month + "-" + (day + 1);
            List<Map> mapList = integralServiceAdmin.selectPlusAndNegativeIntegral(date);
            Integer plus = Integer.parseInt(mapList.get(0).get("plus").toString());
            Integer negative = Integer.parseInt(mapList.get(0).get("negative").toString());

            downloadList.add(plus);
            vipList.add(negative);
            actList.add(plus - negative);

        }

        downloadMap.put("data", downloadList);
        downloadMap.put("name", "总计分数");
        vipMap.put("data", vipList);
        vipMap.put("name", "已销积分数");
        actMap.put("data", actList);
        actMap.put("name", "未销积分数");
        List<Map> yList = new ArrayList<>();
        yList.add(downloadMap);
        yList.add(vipMap);
        yList.add(actMap);
        return BaseResult.ok(Graph.builder().xList(xList).yList(yList).build());

    }


}
